import { Context } from 'koa'
import { insert, select, success } from '../utils'

export class branchController {
  static async getBranchList(ctx: Context) {
    const { system_id } = ctx.request.body as any
    const [result] = (await select('branches', system_id ? { system_id } : {})) as [any, any]
    ctx.body = success(ctx, result)
  }
  static async addBranch(ctx: Context) {
    const { branch, develop_system_id: system_id, git_commit_msg } = ctx.request.body as any
    const [rows]: [any, any] = (await select('systems', { id: system_id })) as [any, any]
    return (await insert('branches', {
      name: rows[0].name + '_' + branch,
      branch,
      system_id,
      modify_time: Date.now(),
      modifier: ctx.request.headers.nickname,
      git_commit_msg,
      coding_developer: ctx.request.headers.nickname,
      coding_developer_token: ctx.cookies.get('token')
    })) as [any, any]
    ctx.body = success(ctx)
  }
  static async deleteBranch(ctx: Context) {}
}
